package com.itgm.jiayun_web_project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itgm.jiayun_web_project.pojo.PageResult;
import com.itgm.jiayun_web_project.pojo.Result;
import com.itgm.jiayun_web_project.pojo.Room;
import com.itgm.jiayun_web_project.service.RoomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/room")
@Slf4j
@Tag(name = "教室接口", description = "教室接口")
public class RoomController {

    @Autowired
    private RoomService roomService;

    //分页查询教室列表
    @GetMapping("/page")
    @Operation(summary = "分页查询教室列表")
    public Result<PageResult<Room>> page(
            @RequestParam Integer page,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String name
    ) {
        log.info("分页查询教室列表");
        //1.获取分页参数
        Page<Room> roomPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Room> rqw = new LambdaQueryWrapper<>();
        if (name != null && !name.equals("")) {
            rqw.like(Room::getName, name);
        }
        //2.分页查询
        Page<Room> page1 = roomService.page(roomPage, rqw);
        PageResult<Room> roomPageResult = new PageResult<>();
        roomPageResult.setTotal(page1.getTotal());
        roomPageResult.setRecords(page1.getRecords());
        return Result.success(roomPageResult);
    }

    //新增教室
    @PostMapping
    @Operation(summary = "新增教室")
    public Result add(@RequestBody Room room) {
        log.info("新增教室");
        boolean save = roomService.save(room);
        if (save) {
            return Result.success();
        }
        return Result.error("新增失败");
    }

    //根据id查询教室
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询教室")
    public Result<Room> getById(@PathVariable Long id) {
        log.info("根据id查询教室");
        Room room = roomService.getById(id);
        if (room != null) {
            return Result.success(room);
        }
        return Result.error("查询失败");
    }

    //修改教室
    @PutMapping
    @Operation(summary = "修改教室")
    public Result update(@RequestBody Room room) {
        log.info("修改教室");
        boolean update = roomService.updateById(room);
        if (update) {
            return Result.success();
        }
        return Result.error("修改失败");
    }


}
